package fourpersons183;

public class CandidateCode {
	private static class Node {
		int v, c = 1, rc = 0;
		Node l, r;
		public Node(int v) {
			this.v = v;
		}
		public int insert(Node n) {
			if(n.v > v) {
				rc++;
				if(r == null) {
					r = n;
					return 0;
				}
				return r.insert(n);
			}
			if(n.v < v) {
				if(l == null) {
					l = n;
					return rc + c;
				}
				return l.insert(n) + rc + c;
			}
			c++;
			return rc;
		}
	}
	
    public static int get_height(String[] input1)
    {
    	Node r = null;
    	int c = 0;
    	for(String h : input1) {
    		String[] t = h.split("#");
    		int f = Integer.parseInt(t[0]);
    		if(f < 4 || f > 7)
    			return -1;
    		int in = Integer.parseInt(t[1]);
    		if(in < 0 || in > 11)
    			return -1;
    		Node n = new Node(f * 12 + in);
    		if(r == null)
    			r = n;
    		else
    			c += r.insert(n);
    	}
    	return c;
    }

	public static void main(String[] args) {
		String[] h1 = {"5#8", "5#7", "6#0", "5#7"};
		System.out.println(get_height(h1));
		String[] h2 = {"4#11","6#0","5#2","6#1","7#1","5#11","5#11","5#10","5#8"};
		System.out.println(get_height(h2));
	}
}
